package com.jz.pay4.bo.impl;

import com.alibaba.fastjson.JSON;
import com.jz.commom.api.dto.request.Payment;
import com.jz.pay4.bo.CommonCacheBO;
import com.jz.pay4.bo.RedisTemplateBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author jz3216
 * @apiNote 设置缓存数据
 */
@Component
public class CommonCacheBOImpl implements CommonCacheBO {

    @Autowired
    RedisTemplateBO redisTemplateBO;

    @Override
    public void setTitle() {
        redisTemplateBO.setString("title","标题1；标题2");
    }

    @Override
    public void cachePayment() {
        String key = "PAYMENT_List";
        if(!redisTemplateBO.exist(key)){
            System.out.println("缓存数据不存在,进行缓存！");
            List<Payment> list = new ArrayList<>();
            list.add(new Payment(9L,"99"));
            list.add(new Payment(8L,"88"));
            String value = JSON.toJSONString(list);

            redisTemplateBO.setString(key, value,100000, TimeUnit.MILLISECONDS);
        }else{
            System.out.println("缓存数据存在,不进行缓存！");
        }

    }

    @Override
    public List<Payment> getCachePayment() {
        String value = redisTemplateBO.getString("PAYMENT_List");
        System.out.println("取出缓存数据: "+value);
        return JSON.parseArray(value,Payment.class);
    }

    @Override
    public Long delCachePayment() {
        Long value = redisTemplateBO.del("PAYMENT_List");
        System.out.println("删除缓存数据: "+value);
        return value;
    }


}
